package com.xfly.app.wxapi;

import android.content.Context;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by Administrator on 2017/3/30.
 */

public class WxPay {

    private IWXAPI api;

    private static WxPay INSTANCE;

    private Context mContext;

    private WxPay(Context context){
        this.mContext = context;
        api = WXAPIFactory.createWXAPI(mContext, "wx971b735e6872c3b7");
    }

    //

    /**
     * 判断支付环境 比如 是否安装了微信 及微信版本是否支持支付功能
     * @return false 环境不好
     */
    public PayEnvironment checkEvironment(){
        PayEnvironment environment = new PayEnvironment();
        //判断是否安装了微信
        boolean isWXInstalled = api.isWXAppInstalled();
        if (!isWXInstalled) {
            environment.isSupport = false;
            environment.error = "微信未安装";
            return environment;
        }
        environment.isSupport = true;
        return environment;
    }


    public static synchronized WxPay getInstance(Context context){
        if (null == INSTANCE) {
            INSTANCE = new WxPay(context);
        }
        return INSTANCE;
    }


    public void pay(PayParams payParams, PayResultListener listener){
        WXPayEntryActivity.setPayResultListener(listener);//设置支付结果回调
        PayReq req = new PayReq();
        req.appId = payParams.appid;//微信开放平台审核通过的应用APPID
        req.partnerId = payParams.partnerid;//微信支付分配的商户号
        req.prepayId= payParams.prepayid;//微信返回的支付交易会话ID
        req.packageValue = payParams.packageValue;//暂填写固定值Sign=WXPay
        req.nonceStr= payParams.noncestr;//随机字符串，不长于32位
        req.timeStamp= payParams.timestamp+"";//时间戳
        req.sign= payParams.sign;//签名
        api.sendReq(req);
    }
}
